package com.adups.monitor.service.impl;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.adups.monitor.config.MonitorParamConfig;
import com.adups.monitor.entity.ExceptionProduct;
import com.adups.monitor.entity.ProInfo;
import com.adups.monitor.entity.StatOTAEntity;
import com.adups.monitor.iotDao.StatOTADao;
import com.adups.monitor.service.EmailService;
import com.adups.monitor.service.StatOTAService;

//##1.下载率统计
//threshold.monitor.strategy0_downPercentage=0.6
//threshold.monitor.strategy1_downPercentage=0.8
//
//##2.升级率统计
//#第一种统计公式：升级率=升级成功/检测数 
//threshold.monitor.strategy0_upPercentage=0.6
//threshold.monitor.strategy1_upPercentage=0.8
//threshold.monitor.checkNum=1000
//
//#第二种统计公式：升级率=升级成功/下载成功
//threshold.monitor.upPercentage=0.9
//threshold.monitor.downNum=1000

@Service("statOTAService")
public class StatOTAServiceImpl implements StatOTAService{
	
	@Autowired 
	private MonitorParamConfig monitorParamConfig;

	@Autowired
	private StatOTADao statOTADao;

	@Autowired
	private EmailService emailService;
	
	private static Log logger = LogFactory.getLog(StatOTAServiceImpl.class);

	private DecimalFormat    df   = new DecimalFormat("######0.00"); 

	public void myMonitorJob() {

		List<StatOTAEntity> list = statOTADao.getList();
		
		List<StatOTAEntity> result = new ArrayList<StatOTAEntity>();
		
		if(list == null || list.size() == 0 ){
			return;
		}
		
		for(StatOTAEntity bean : list){

			Integer strategy = bean.getIsStrategy();//策略：       0:完整版            1:精简版
			
			//1、【完整版】
			if(strategy == 0){
				if(
						//1.1、下载率=下载成功/检测数    小于等于60%且检测数大于等于1000
						(bean.getCountCheck()>=monitorParamConfig.getCheckNum() && bean.getCountDownload()*1.0000/bean.getCountCheck() < monitorParamConfig.getStrategy0DownPercentage()) ||
						//1.2、 计算方式一：升级率=升级成功/检测数    小于等于60%且检测数大于等于1000
						(bean.getCountCheck()>=monitorParamConfig.getCheckNum() && bean.getCountUpgrade()*1.0000/bean.getCountCheck() < monitorParamConfig.getStrategy0UpPercentage()) ||
                        //     计算方式二：升级率 = 升级成功/下载成功    小于等于90% 且总升级次数大于等于1000
						(bean.getCountUpgrade()+bean.getCountUpgradefail()>= monitorParamConfig.getUpNum()  && bean.getCountUpgrade()*1.0000/bean.getCountDownload() < monitorParamConfig.getUpPercentage())
				){
					result.add(bean);
				}
			}
			//2、【精简版】
			if(strategy == 1){
				if(
						//2.1、下载率=下载成功/检测数    小于等于80%且检测数大于等于1000
						(bean.getCountCheck()>= monitorParamConfig.getCheckNum() && bean.getCountDownload()*1.0000/bean.getCountCheck() < monitorParamConfig.getStrategy1DownPercentage()) ||
						//2.2、 计算方式一：升级率=升级成功/检测数    小于等于80%且检测数大于等于1000
						(bean.getCountCheck()>= monitorParamConfig.getCheckNum() && bean.getCountUpgrade()*1.0000/bean.getCountCheck() < monitorParamConfig.getStrategy1UpPercentage()) ||
                        //     计算方式二：升级率 = 升级成功/下载成功    小于等于90% 且总升级次数大于等于1000
						(bean.getCountUpgrade() + bean.getCountUpgradefail() >= monitorParamConfig.getUpNum()  && bean.getCountUpgrade()*1.0000/bean.getCountDownload() < monitorParamConfig.getUpPercentage())	
				){
					 result.add(bean);
				}
			}
			
			//3、既不是完整版，也不是精简版
			if(strategy != 0 && strategy != 1){
				logger.info("策略值有误(0:完整版     1:精简版),strategy="+strategy +"; "+bean);
			}
		}	
		
//		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		if(result == null || result.size() == 0 ){
			return;
		}
		
		String content = "<p>Dear all:</p>"
						+ "<p>&nbsp;&nbsp;物联网云平台"+new Date()+"存在升级异常情况，请相关的工作人员尽快排查解决。</p>"
						+ "<p>&nbsp;&nbsp;具体异常情况，如下：</p>";
		String table1 = "<table border='1',cellspacing='0', width = '70%'>"
								+ "<thead>"
									+ "<tr>"
										+ "<td>账号(email)</td>"
										+ "<td>项目名称</td>"
										+ "<td>源版本</td>"
										+ "<td>目标版本</td>"
										+ "<td>差分包大小(单位:bit)</td>"
										+ "<td>检测总数</td>"
										+ "<td>下载总数</td>"
										+ "<td>下载成功数</td>"
										+ "<td>下载率(下载成功/检测数*100%)</td>"
										+ "<td>升级总数</td>"
										+ "<td>升级成功数</td>"
										+ "<td>升级率(升级成功/检测数*100%)</td>"
										+ "<td>升级率(升级成功/下载成功*100%)</td>"
									+ "</tr>"
								+ "</thead>";
		
		String table2 = "<tbody>";
		for(StatOTAEntity bean : result){
			
			ProInfo params = new ProInfo();
			params.setProductId(bean.getProductId());
			params.setDeltaId(bean.getDeltaId());
			ProInfo info =  statOTADao.getProInfo(params);
			
			
			table2 += "<tr>"
					+ "<td>"+info.getEmail()+"</td>"   //账号
					+ "<td>"+info.getProductName()+"</td>"   //项目名称
					+ "<td>"+bean.getOriginVersion()+"</td>" //源版本
					+ "<td>"+bean.getTargetVersion()+"</td>"  //目标版本
					+ "<td>"+info.getDeltaSize()+"</td>"   //差分包大小
					+ "<td>"+bean.getCountCheck()+"</td>"  //下载数
					+ "<td>"+(bean.getCountDownload() + bean.getCountDownfail())+"</td>"  //下载数
					+ "<td>"+bean.getCountDownload()+"</td>"    //下载成功数
					+ "<td>"+df.format(bean.getCountDownload()*1.0000*100/bean.getCountCheck())+"</td>"    //下载率(下载成功/检测数)
					+ "<td>"+(bean.getCountUpgrade() + bean.getCountUpgradefail())+"</td>"    //升级数
					+ "<td>"+bean.getCountUpgrade()+"</td>"    //升级成功数
					+ "<td>"+df.format(bean.getCountUpgrade()*1.0000*100/bean.getCountCheck())+"</td>"    //升级率(升级成功/检测数)
					+ "<td>"+df.format(bean.getCountUpgrade()*1.0000*100/bean.getCountDownload())+"</td>"    //升级率(升级成功/下载成功)
					+ "</tr>";
		}
		
		String table3 = "</tbody>" + "</table>"; 
		
		String message = content + table1 + table2 + table3;
		
		emailService.sendHtmlMessage( "异常项目监控", message);
		
	}
	
	
	public void testQuartz(){
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>"+new Date());
	}
	
}
